import 'dart:io';

//更新文件名----更新文件内容
void main() {
  // 当前脚本所在目录
  final scriptDir = File(Platform.script.toFilePath()).parent;
  final dirName = scriptDir.uri.pathSegments.lastWhere((e) => e.isNotEmpty);

  String parentStateFileName = 'parentStateFileName';
  print('当前目录: ${scriptDir.path}');
  print('目录名: $dirName');

  //1.修改文件名称
  List<String> newNames = changeFileName(dirName,scriptDir);
//   2.修改文件内容
  changeContent(scriptDir, newNames);

}
changeContent(Directory scriptDir,List<String> newNames){
  // 1.取名称
  var parentStateName = findParentStateFileName(scriptDir);
  final dirName = scriptDir.uri.pathSegments.lastWhere((e) => e.isNotEmpty);

  //   2.修改import位置
  for (var entity in scriptDir.listSync()) {
    if (entity is File && entity.path.endsWith('.dart')) {
      final fileName = entity.uri.pathSegments.last;
      // 跳过脚本自身
      if (fileName == 'rename2.dart') continue;
      if (fileName == newNames[0]) {
        final content = entity.readAsStringSync();
        // //替换 import 部分
        final replaced1 = content.replaceAll('template_state.dart', parentStateName);
        final replaced2 = replaced1.replaceAll('templateState', toCamelCase(parentStateName.split('.').first));
        final replaced3 = replaced2.replaceAll('widgetLogic', toCamelCase(fileName.split('.').first));
        entity.writeAsStringSync(replaced3);
      };
      if (fileName == newNames[1]) {
        final content = entity.readAsStringSync();
        // //替换 import 部分
        final replaced1 = content.replaceAll('widget_logic.dart', newNames[0]);
        final replaced2 = replaced1.replaceAll('widget', toCamelCase(dirName));
        final replaced3 = replaced2.replaceAll('widgetLogic', toCamelCase(newNames[1].split('.').first));
        entity.writeAsStringSync(replaced2);
      };
      print('JtTag:fileName:${fileName}');


    }
  }


}
List<String> changeFileName(String dirName,Directory scriptDir){
  List<String> newFileNames =[];
  // 遍历目录下的文件
  for (var entity in scriptDir.listSync()) {
    if (entity is File && entity.path.endsWith('.dart')) {
      final fileName = entity.uri.pathSegments.last;
      // 跳过脚本自身
      if (fileName == 'rename2.dart') continue;

      print('JtTag:fileName:${fileName}');
      var suffix = fileName.split('_').last;
      final newName = '${dirName}_$suffix';
      final newPath = '${scriptDir.path}/$newName';

      entity.renameSync(newPath);
      print('重命名: $fileName → $newName');
      newFileNames.add(newName);
    }
  }
  return newFileNames;
}
String toCamelCase(String input) {
  // 全小写 + 按 "_" 分割
  final parts = input.toLowerCase().split('_');
  return parts.skip(0).map((word) {
    if (word.isEmpty) return '';
    return word[0].toUpperCase() + word.substring(1);
  }).join();
}

//找到外部state文件名称
String findParentStateFileName(Directory scriptDir){
  var Dir = scriptDir.parent.parent;
  var ParentStateFileName = '';
  for (var entity in Dir.listSync()) {
    if (entity is File && entity.path.endsWith('.dart')) {
      final fileName = entity.uri.pathSegments.last;
      if(fileName.contains('state')){
        ParentStateFileName = fileName;
        print('JtTag:查看名称:${fileName}');
      }

    }
  }
  return ParentStateFileName;
}
